package com.wanxi.dao;

import com.wanxi.model.BrandModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class BrandDao {
    private static List<BrandModel> list = new ArrayList<>();

    static {
        list.add(new BrandModel(1, "aa", "aa", 1, "aa", 1, 1));
        list.add(new BrandModel(2, "aa", "aa", 1, "aa", 1, 1));
        list.add(new BrandModel(3, "aa", "aa", 1, "aa", 1, 1));
    }

    public int add(BrandModel model) {
        model.setId(list.size() + 1);
        list.add(model);
        return 1;
    }

    public List<BrandModel> find() {
        return list;
    }

    public int delete(BrandModel model) {
        Iterator<BrandModel> iterator = list.iterator();
        while (iterator.hasNext()) {
            int id = iterator.next().getId();
            if (id == model.getId()) {
                iterator.remove();
                break;
            }
        }
        return 1;
    }

    public BrandModel findById(BrandModel model) {
        Iterator<BrandModel> iterator = list.iterator();
        while (iterator.hasNext()) {
            BrandModel brandModel = iterator.next();
            int id = brandModel.getId();
            if (id == model.getId()) {
                return brandModel;
            }
        }
        return null;
    }

    public int update(BrandModel model) {
        Iterator<BrandModel> iterator = list.iterator();
        while (iterator.hasNext()) {
            BrandModel brandModel = iterator.next();
            int id = brandModel.getId();
            if (id == model.getId()) {
                iterator.remove();
            }
        }
        list.add(model);
        return 0;
    }
}
